package com.example.webpractice.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.webpractice.dao.ZuoyexinxiDao;
import com.example.webpractice.dto.ZuoyexinxiDTO;
import com.example.webpractice.po.ZuoyexinxiPO;
import com.example.webpractice.vo.PageVo;
import com.example.webpractice.vo.Po2Vo;
import com.example.webpractice.vo.ZuoyexinxiDetailVO;
import com.example.webpractice.vo.ZuoyexinxiListVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service("zuoyexinxiService")
public class ZuoyexinxiService {

    @Autowired
    private ZuoyexinxiDao dao;

    public PageVo queryPage(int pages, int limit, QueryWrapper queryWrapper) {
        Page<ZuoyexinxiPO> pagePO = dao.selectPage(new Page<>(pages, limit), queryWrapper);
        return Po2Vo.toVoPage(pagePO, ZuoyexinxiListVO.class);
    }

    public ZuoyexinxiDetailVO selectById(Long id) {
        ZuoyexinxiPO po = dao.selectOne(new QueryWrapper<ZuoyexinxiPO>().eq("id", id));
        ZuoyexinxiDetailVO vo = new ZuoyexinxiDetailVO();
        BeanUtils.copyProperties(po, vo);
        return vo;
    }

    public int deleteBatchIds(List list) {
        return dao.deleteByIds(list);
    }


    public ZuoyexinxiDetailVO selectByNameAndXuehao(String name,
                                                    String xuehao) {
        QueryWrapper queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("kechengmingcheng", name);
        queryWrapper.eq("xuehao", xuehao);
        ZuoyexinxiPO po = new ZuoyexinxiPO();
        po = dao.selectOne(queryWrapper);
        if (po == null) {
            return null;
        } else {
            ZuoyexinxiDetailVO vo = new ZuoyexinxiDetailVO();
            BeanUtils.copyProperties(po, vo);
            return vo;
        }
    }

    public int create(ZuoyexinxiDTO dto) {
        ZuoyexinxiPO po = new ZuoyexinxiPO();
        BeanUtils.copyProperties(dto, po);
        return dao.insert(po);
    }

    public int update(ZuoyexinxiDTO zuoyexinxiDTO) {
        ZuoyexinxiPO po = new ZuoyexinxiPO();
        BeanUtils.copyProperties(zuoyexinxiDTO, po);
        return dao.updateById(po);
    }
}